/*******************    (C) COPYRIGHT SUNSHINE SILICON    **********************
* \file
* File Name: soft_spi.c
* \brief
* Description:

    soft_spi
    
*  Copyright (C) 2014-2035 Sunshine Silicon Corporation (www.sunsili.com), All Rights Reserved
*  SPDX-License-Identifier: Apache-2.0
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may
*  not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*  This file is part of HS6200 SDK
*
* Version && Reversed History
* Version    Date       Auther      Reversed History
  ----------------------------------------------------------------------------
  V1.0.0    2021-07-07  Lojam Fan  Fisrt Created 
  
  (C) Sunshine Silicon Corporation (www.sunsili.com)
*******************************************************************************/

#include "soft_spi.h"
#include "config.h"


/*******************************************************************************
* Function: soft_spi_init
* Return:   void
* Param:    void
* Description::
    soft_spi init

* Version    Date       Auther      Reversed History
  ---------------------------------------------------------------------------
  V1.0.0    2021-10-20  Lojam Fan  First Created 

* FILE: soft_spi.c

*******************************************************************************/
void soft_spi_init ( void )
{
    setGPIO_SPI_CE_OUT();
    setGPIO_SPI_CS_OUT();
    setGPIO_SPI_SCK_OUT();
    setGPIO_SPI_MOSI_OUT();
    setGPIO_SPI_MISO_IN();
}


/*******************************************************************************
* Function: soft_spi_rw
* Return:   unsigned char
* Param:    unsigned char value
* Description::
    Soft spi read and write
* Version    Date       Auther      Reversed History
  ---------------------------------------------------------------------------
  V1.0.0    2021-10-20  Lojam Fan  First Created 

* FILE: soft_spi.c

*******************************************************************************/

unsigned char soft_spi_rw(unsigned char value)                                    
{                                                           
	unsigned char bit_ctr;
    
	for(bit_ctr=0; bit_ctr<8; bit_ctr++)   // output 8-bit
	{
		if((value & 0x80)==0X80)
		{
//			MOSI=1;
            SPI_MOSI_HIGH();
		}
		else
		{
//			MOSI=0;		
            SPI_MOSI_LOW();
		}

		value = (value << 1);           // shift next bit into MSB..
//		SCK = 1;                      // Set SCK high..
        SPI_SCK_HIGH() ;
//	    if(MISO)
        if(getGPIO(SPI_MISO))
		   value |= 1;       		  // capture current MISO bit
//	    else
//		   value |= 0;

//		SCK = 0;            		  // ..then set SCK low again
        SPI_SCK_LOW() ;
	}
	return(value);           		  // return read UINT8
} 



/******************* (C) COPYRIGHT SUNSHINE SILICON  **************************
********************        END OF FILE HS6200_demo.c        *******************/